package vn.sunnet.game;

import javax.microedition.lcdui.game.GameCanvas;
import java.util.Timer;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Honghai
 * Sunnet.vn
 */


public class SnSplashScreen extends Canvas {

    private Display display;
    private Displayable next;
    private Timer timer;
    private Image image;
    private int dismissTime;

    public SnSplashScreen(Display display, Displayable next, Image image, int dismissTime) {
        timer = new Timer();
        this.display = display;
        this.next = next;
        this.image = image;
        this.dismissTime = dismissTime;
        display.setCurrent(this);
    }

    static void access(SnSplashScreen splashScreen) {
        splashScreen.dismiss();
    }

    private void dismiss() {
        timer.cancel();
        display.setCurrent(next);
    }

    protected void keyPressed(int keyCode) {
        dismiss();
    }

    protected void paint(Graphics g) {
        g.setColor(0x00FFFFFF);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x00000000);
        g.drawImage(image, getWidth() / 2, getHeight() / 2 - 5, 3);
    }

    protected void pointerPressed(int x, int y) {
        dismiss();
    }

    protected void showNotify() {
        if (dismissTime > 0) {
            timer.schedule(new CountDown(this), dismissTime);
        }
    }
}
